/*
	Copyright (C) 2008 by Jean-FranÁois Geyelin
	jfgeyelin@gmail.com

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the
	Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "Goomba.h"
#include "globals.h"
#include "FloatingMessage.h"

Goomba::Goomba(float x_, float y_): Enemy(x_,y_,28,22,4,5,20,14) {
	sprites = new SplittedSurface("goomba.png",14);
	timeLeftUntilNextFrame=2;
	direction=RIGHT;
	previousX=0;
	typeOfEnemy = GOOMBA_EN;
}



Goomba::Goomba(float x_, float y_, std::string str): Enemy(x_,y_,28,22,7,5,14,14) {
    sprites = new SplittedSurface("goomba.png",14);
	timeLeftUntilNextFrame=2;
	if (str=="right") {
		direction=RIGHT;
	}
	else
	{
		direction=LEFT;
	}
	typeOfEnemy = GOOMBA_EN;
}

Goomba::~Goomba(void) {
    delete sprites;
}

void Goomba::killGoomba(void) {
    isDead = true;
    deathFrame = 0;
    timeLeftUntilNextFrame = 6;
}


void Goomba::handleCollisionWithObject(Object *o) {
	if (o->type != MOVING_OBJECT)
	return;

    if (((MovingObject*)o)->typeOfMovingObject == ENEMY_MO) {
        if (((Enemy*)o)->typeOfEnemy == GOOMBA_EN) {
            if (direction == LEFT) {
                direction = RIGHT;

            }
            else {
                direction = LEFT;
            }
        }
    }


	if (o==player) {
        if (isDead)
        return;

    if (playerIsJumpingOnThisObject()) {
			isDead = true;
			deathFrame = 0;
			timeLeftUntilNextFrame = 6;
			player->setVy(-3);
			player->increaseNumberOfPoints(100);
			new FloatingMessage(x+4,y,"p_100.png");
			allSounds->play("kick");
			player->setCanJump();
		}
		else
		{
			player->setAlive(false);
		}
	}
}




void Goomba::setVxVy(void) {
	
	if (!isDead && abs((int)(player->getX() - this->x)) < 40) {
		if (player->spearIsHittingThisObject(this)) {
			isDead = true;
			deathFrame = 0;
			timeLeftUntilNextFrame = 6;
			player->increaseNumberOfPoints(100);
			new FloatingMessage(x+4,y,"p_100.png");			
		}
	}
	
	
	
	timeLeftUntilNextFrame--;
	vx = 0;
	int v = map->getForeGround()->getPlane()[((int)x+14)/16+(((int)y+11)/16)*mapWidth];
	if (!isDead) {
		if (direction==RIGHT) {
			if (!timeLeftUntilNextFrame) {
				frameToBeDrawn++;
				if (frameToBeDrawn > 6)
				frameToBeDrawn = 0;
				timeLeftUntilNextFrame = 8;
			}
			if (frameToBeDrawn!=0 && frameToBeDrawn!=6)
			vx = 0.3;
			else
			vx = 0.1;
			x++;
			if (collides(p.E) || v == B_GOOMBA_GO_LEFT2-1)
			direction = LEFT;
			x--;
		}
		else
		{
			if (!timeLeftUntilNextFrame) {
				frameToBeDrawn--;
				if (frameToBeDrawn < 0)
				frameToBeDrawn = 5;
				timeLeftUntilNextFrame = 8;
			}
			if (frameToBeDrawn!=0 && frameToBeDrawn!=6)
			vx = -0.3;
			else
			vx = -0.1;
			x--;
			if (collides(p.L) || v == B_GOOMBA_GO_RIGHT2-1)
			direction = RIGHT;
			x++;
		}
	}
	else
	{
		frameToBeDrawn = deathFrame+8;
		if (!timeLeftUntilNextFrame) {
			deathFrame++;
			timeLeftUntilNextFrame = (deathFrame!=4?4:8);
			if (deathFrame==5)
			mustBeDestroyed = true;
		}
	}
}
